package socketmvc.core.util.xpath;

public class FastPathMatcher extends AbstractPathMatcher{
    /**
     * 字符串与通配符进行比较
     *
     * @param path     路径字符串
     * @param wildcard 匹配符/通配符
     * @return 匹配结果 true 代表匹配成功
     */
    @Override
    protected boolean doMatch(String path, String wildcard) {
        int i = 0;
        int j = 0;
        int starIndex = -1;
        int iIndex = -1;

        while (i < path.length()) {
            if (j < wildcard.length() && (wildcard.charAt(j) == '?' || wildcard.charAt(j) == path.charAt(i))) {
                ++i;
                ++j;
            } else if (j < wildcard.length() && wildcard.charAt(j) == '*') {
                starIndex = j;
                iIndex = i;
                j++;//'*' can match 0 or above 0 characters
            } else if (starIndex != -1) {
                //such as "abggggb","*b"
                //so every time matching starts form the first index of *
                //can avoid the case above
                j = starIndex + 1;
                i = iIndex + 1;
                iIndex++;
            } else {
                return false;
            }
        }

        while (j < wildcard.length() && wildcard.charAt(j) == '*') {
            ++j;
        }

        return j == wildcard.length();
    }

    public static void main(String[] args) {
        FastPathMatcher regexPathMatcher = new FastPathMatcher();
        System.out.println(regexPathMatcher.match("/backend/utils/a.js","/**"));
    }
}
